#include <msp430f6638.h>


/**
 * main.c
 */

    unsigned int duty[10] = {50,150,250,350,450,550,650,750,850,950};

    unsigned char point = 5;
    unsigned char point1 = 5;
void delay_ms(unsigned int duration);
unsigned char Is_sw_pressed(unsigned char key_bit);

int main(void)
{


	WDTCTL = WDTPW | WDTHOLD;	// stop watchdog timer

	while(BAKCTL & LOCKIO)  BAKCTL &= ~(LOCKIO);

	UCSCTL6 &= ~(XT1OFF);

	UCSCTL6 |= XCAP_3;


	do{
	    UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
	    SFRIFG1 &= ~OFIFG;

	}while(SFRIFG1 & OFIFG);

	UCSCTL4 |= SELA_0;

	P4DIR |= BIT4+BIT5;
	P4SEL |= BIT4+BIT5;
	P4DIR &= ~(BIT2+BIT3);

	TB0CCR0 = 1024-1;
	TB0CCTL4 = OUTMOD_7;
	TB0CCR4 = duty[point];
	TB0CTL = TBSSEL_1 + MC_1 + TBCLR;
	TB0CCTL5 = OUTMOD_7;
	TB0CCR5 = duty[point1];




    P4IE|=BIT2+BIT3;
    P4IES|=BIT2+BIT3;
    P4IFG &=~(BIT2+BIT3);
    _BIS_SR(GIE);

//	while(1){
//	    if(Is_sw_pressed(2)==1){
//	        if(point !=9){
//	            TB0CTL &= 0xffcf;
//	            TB0CCR4 = duty[++point];
//	            TB0CTL |= MC_1 + TBCLR;
//	        }
//	    }
//	    else if(Is_sw_pressed(3)==1){
//	        if(point != 0){
//	            TB0CTL &= 0xffcf;
//	            TB0CCR4 = duty[--point];
//	            TB0CTL |= MC_1 + TBCLR;
//	        }
//	    }
//	}
}

void delay_ms(unsigned int duration){
    volatile unsigned int i;
    for(i=0; i < 5000*duration; i++){};
}


#pragma vector = PORT4_VECTOR
__interrupt void PORT4_ISR(void){
    if((P4IFG&BIT2)!=0){
        if(point !=9 && point1 !=0){
            TB0CTL &= 0xffcf;
            TB0CCR4 = duty[++point];

            TB0CCR5 = duty[--point1];
            TB0CTL |= MC_1 + TBCLR;



        }
        P4IFG &=~BIT2;

    }
    else{
        if(point != 0 && point1 != 9){
            TB0CTL &= 0xffcf;
            TB0CCR4 = duty[--point];
            TB0CCR5 = duty[++point1];
            TB0CTL |= MC_1 + TBCLR;
        }
        P4IFG &=~BIT3;
    }


}

